package com.rn.gypsophila.redis.web;

import com.google.common.base.Charsets;
import com.google.common.hash.Funnel;
import com.rn.gypsophila.mvc.BaseController;
import com.rn.gypsophila.redis.basic.BloomFilter;
import com.rn.gypsophila.redis.basic.RedisUtils;
import com.rn.gypsophila.redis.model.Simple;
import com.rn.gypsophila.redis.queue.blocking.RedisMessageProducer;
import com.rn.gypsophila.redis.queue.pubsub.MessagePublisher;
import com.rn.gypsophila.model.GlobalRet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * Description: 测试
 *
 * @author 然诺
 * @date 2020/05/09
 */
@Api(tags = {"测试服务接口"})
@RestController
@AllArgsConstructor
@RequestMapping("test")
public class TestController extends BaseController {

	private final MessagePublisher messagePublisher;
	private final RedisMessageProducer redisMessageProducer;
	private final RedisUtils redisUtils;

	private static BloomFilter<String> bloomFilter;
	static {
		bloomFilter = new BloomFilter<>((Funnel<String>) (from, into) -> into.putString(from, Charsets.UTF_8)
				.putString(from, Charsets.UTF_8), 0.001, 1000000);
	}

	@ApiOperation(value = "普通队列")
	@GetMapping("queue/send")
	public GlobalRet send() {
		redisMessageProducer.send("hello world" + UUID.randomUUID().toString());
		return success();
	}

	@ApiOperation(value = "Pub/Sub功能")
	@GetMapping("pubsub/publish")
	public GlobalRet publish() {
		messagePublisher.publish("hello world");
		return success();
	}

	@ApiOperation(value = "布隆过滤器-添加值")
	@PostMapping("bloom/add")
	public GlobalRet addByBloomFilter(@RequestBody Simple simple) {
		redisUtils.addByBloomFilter(bloomFilter, "simple_id", simple.getId());
		return success();
	}

	@ApiOperation(value = "布隆过滤器-判断是否存在值")
	@ApiImplicitParam(name = "id", value = "ID", required = true, example = "1")
	@GetMapping("bloom/include")
	public GlobalRet includeByBloomFilter(@RequestParam String id) {
		boolean include = redisUtils.includeByBloomFilter(bloomFilter, "simple_id", id);
		return success(include);
	}

}
